#include "ULGfx_Serial.h"
#include <SDL/sprig.h>

ULG_Serial::ULG_Serial():initFlags(0), balls(NULL)
{
}

ULG_Serial::ULG_Serial(VSDL_Point point):initFlags(0), balls(NULL)
{
	SetXY(point);
}

ULG_Serial::ULG_Serial(VSDL_Point point, VSDL_Font *font):initFlags(0), balls(NULL)
{
	SetXY(point);
	SetTipFont(font);
}

void ULG_Serial::SetXY(VSDL_Point point)
{
	startXY = point;
}

void ULG_Serial::SetULGBalls(ULG_Balls *ulgBalls)
{
	if (ulgBalls == NULL){
		return ;
	}

	balls = ulgBalls;
	initFlags |= ULG_SERIAL_F_ULG_BALLS_SET;
}

void ULG_Serial::SetTipFont(VSDL_Font *font)
{
	if (font == NULL){
		return ;
	}

	msg.SetFont(font);
	msg.SetEncode(FONT_CODE_UTF8);
	initFlags |= ULG_SERIAL_F_TIP_FONT_SET;
}

int ULG_Serial::Show(SDL_Surface *destSurf)
{
	VSDL_Point currentXY;
	char msgBuffer[128];
	int w, h;
	int ballH;
	VSDL_RGB rgb(55, 55, 55);

	if (destSurf == NULL || (initFlags & ULG_SERIAL_F_TIP_FONT_SET) == 0 || (initFlags & ULG_SERIAL_F_ULG_BALLS_SET) == 0){
		return -1;
	}
	
	currentXY = startXY;

	//Show serial number
	sprintf(msgBuffer, "期号: %d", sn);
	msg.SetText(msgBuffer);
	//Use First tip's font height because font height is fixed
	msg.GetTextSize(&w, &h);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + h, rgb.GetRGB32(destSurf));
	msg.Blit(destSurf, currentXY);

	//Show serial date
	currentXY.y += h;
	sprintf(msgBuffer, "日期: %s", date);
	msg.SetText(msgBuffer);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + h, rgb.GetRGB32(destSurf));
	msg.Blit(destSurf, currentXY);

	if (balls->GetBlueBallImageHeight() <= balls->GetRedBallImageHeight()){
		ballH = balls->GetRedBallImageHeight();
	}else{
		ballH = balls->GetBlueBallImageHeight();
	}

	//Show sortedBalls
	currentXY.y += h;
	strcpy(msgBuffer, "最终号码:");
	msg.SetText(msgBuffer);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + h, rgb.GetRGB32(destSurf));
	msg.Blit(destSurf, currentXY);

	currentXY.y += h;
	*balls = sortedBalls;
	balls->SetFirstBallXY(currentXY);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + balls->GetRedBallImageHeight(), rgb.GetRGB32(destSurf));
	balls->Show(destSurf);

	//Add ball image's height
	currentXY.y += ballH;
	//Show balls ordered by out
	strcpy(msgBuffer, "出球顺序:");
	msg.SetText(msgBuffer);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + h, rgb.GetRGB32(destSurf));
	msg.Blit(destSurf, currentXY);
	currentXY.y += h;

	*balls = actualOutBalls;
	balls->SetFirstBallXY(currentXY);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + balls->GetRedBallImageHeight(), rgb.GetRGB32(destSurf));
	balls->Show(destSurf);
	
	//Add ball image's height
	currentXY.y += ballH;
	//Show prizes 
	sprintf(msgBuffer, "% -10d 注一等奖 (RMB %10d)", top.hits, top.prize);
	msg.SetText(msgBuffer);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + balls->GetRedBallImageHeight(), rgb.GetRGB32(destSurf));
	msg.Blit(destSurf, currentXY);
	currentXY.y += ballH;

	sprintf(msgBuffer, "% -10d 注二等奖 (RMB %10d)", second.hits, second.prize);
	msg.SetText(msgBuffer);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + balls->GetRedBallImageHeight(), rgb.GetRGB32(destSurf));
	msg.Blit(destSurf, currentXY);
	currentXY.y += ballH;
	
	sprintf(msgBuffer, "% -10d 注三等奖 (RMB %10d)", third.hits, third.prize);
	msg.SetText(msgBuffer);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + balls->GetRedBallImageHeight(), rgb.GetRGB32(destSurf));
	msg.Blit(destSurf, currentXY);
	currentXY.y += ballH;

	sprintf(msgBuffer, "% -10d 注四等奖 (RMB %10d)", fourth.hits, fourth.prize);
	msg.SetText(msgBuffer);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + balls->GetRedBallImageHeight(), rgb.GetRGB32(destSurf));
	msg.Blit(destSurf, currentXY);
	currentXY.y += ballH;

	sprintf(msgBuffer, "% -10d 注五等奖 (RMB %10d)", fifth.hits, fifth.prize);
	msg.SetText(msgBuffer);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + balls->GetRedBallImageHeight(), rgb.GetRGB32(destSurf));
	msg.Blit(destSurf, currentXY);
	currentXY.y += ballH;

	sprintf(msgBuffer, "% -10d 注六等奖 (RMB %10d)", sixth.hits, sixth.prize);
	msg.SetText(msgBuffer);
	//Draw Rect
	SPG_Rect(destSurf, currentXY.x, currentXY.y
			, currentXY.x + balls->GetRedBallImageWidth() * 7, currentXY.y + balls->GetRedBallImageHeight(), rgb.GetRGB32(destSurf));
	msg.Blit(destSurf, currentXY);

	return 0;
}

ULG_Serial &ULG_Serial::operator=(const ULC_Serial &ulcSerial)
{
	sn = ulcSerial.sn;
	strncpy(date, ulcSerial.date, DATE_ALIGN_SIZE);
	sortedBalls = ulcSerial.sortedBalls;	
	actualOutBalls = ulcSerial.actualOutBalls;
	//Don't care extraData for now
	top = ulcSerial.top;
	second = ulcSerial.second;
	third = ulcSerial.third;
	fourth = ulcSerial.fourth;
	fifth = ulcSerial.fifth;
	sixth = ulcSerial.sixth;
}

ULG_Serial &ULG_Serial::operator=(const ULG_Serial &ulgSerial)
{
	*this = static_cast<ULC_Serial>(ulgSerial);

	return *this;
}
